package com.gmu.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gmu.pojo.TblStudent;
import com.gmu.pojo.TblUnits;
import com.gmu.pojo.TblUser;
import com.gmu.pojo.excel.UserData;
import com.gmu.service.TblStudentService;
import com.gmu.service.TblUnitsService;
import com.gmu.service.TblUserService;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;

public class ExcelListener extends AnalysisEventListener<UserData> {

    //因为UserExcelListener不能交给spring管理，所以需要手动注入
    public TblUserService tblUserService;

    public TblStudentService tblStudentService;

    public PasswordEncoder passwordEncoder;

    public TblUnitsService unitsService;


    public ExcelListener(TblUserService tblUserService,TblStudentService tblStudentService,TblUnitsService unitsService,PasswordEncoder passwordEncoder) {
        this.tblUserService = tblUserService;
        this.tblStudentService = tblStudentService;
        this.unitsService = unitsService;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public void invoke(UserData userData, AnalysisContext analysisContext) {
        QueryWrapper<TblUnits> wrapper = new QueryWrapper<>();
        wrapper.eq("name",userData.getClassName());
        Integer unitsId = unitsService.getOne(wrapper).getUnitsId();
        //添加进user表
        TblUser user = new TblUser();
//        //判断该角色是学生还是教师，0为学生，1为教师，默认为学生
//        int role = userData.getRoleId();
//        String idNumber = userData.getIdNumber();
//        user.setIdNumber(idNumber);
//        String telephone = userData.getTelephone();
//        user.setTelephone(telephone);
//        user.setPassword(userData.getPassword());
//        user.setRoleId(userData.getRoleId());
//        user.setDelFlag(userData.getDelFlag());
        BeanUtils.copyProperties(userData,user);
        user.setRoleId(1);
        user.setDelFlag(0);
        user.setPassword(passwordEncoder.encode(userData.getPassword()));
        user.setUnitsId(unitsId);
        tblUserService.save(user);
        //查询生成的userId
        int userId = user.getUserId();
        //添加进student表
        TblStudent student = new TblStudent();
//        student.setTelephone(telephone);
//        student.setIdNumber(idNumber);
//        student.setName(userData.getName());
//        student.setSex(userData.getSex());
//        student.setAcademy(userData.getAcademy());
//        student.setAddress(userData.getAddress());
//        student.setClassName(userData.getClassName());
//        student.setGrade(userData.getGrade());
//        student.setMajor(userData.getMajor());
//        student.setSchool(userData.getSchool());
//        student.setStudentNumber(userData.getStudentNumber());
//        student.setAvatar(userData.getAvatar());
//        student.setDelFlag(userData.getDelFlag());
        BeanUtils.copyProperties(userData,student);
        student.setUserId(userId);
        student.setUnitsId(unitsId);
        tblStudentService.save(student);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
